<?php
function mi_update($table,$columns,$limit,$db_id="default"){
	global $dbs , $configs ;
	if(empty($columns)){
		if($configs["product_mode"] == false){
			print "没有定义columns的值<br>";
		}
		return false ;
	}
	$keys = array_keys($columns);
	$sql = make_prepare_update_sql($table,$keys,$limit) ;
	$params = array_values($columns);
	// print_html($sql) ;

	$mysqli = new mysqli($dbs[$db_id]["host"],$dbs[$db_id]["user"],$dbs[$db_id]["pass"],$dbs[$db_id]["db_name"]);
	if (mysqli_connect_errno()) {
		print "bbb <br>";
		if($configs["product_mode"] == false){
			printf("Connect failed: %s\n", mysqli_connect_error());
		}
		return false ;
	}
	$result = false ;
	// $first = $params[0];

	if ($stmt = $mysqli->prepare($sql)) {
		$matrix = array();
		for($i=0;$i<count($params);$i++){
			$matrix["param_".$i] = $params[$i] ;
		}
		$args = to_binds(&$matrix);
		call_user_func_array(array(&$stmt, 'bind_param'), $args); 
		$stmt->execute();
		$result = $stmt->affected_rows ;
		$stmt->close();
	}

	$mysqli->close();
	return $result ;
}

function mi_insert($table,$columns,$db_id="default",$insert_ignore=false){
	global $dbs , $configs ;
	if(empty($columns)){
		if($configs["product_mode"] == false){
			print "没有定义columns的值<br>";
		}
		return false ;
	}
	$keys = array_keys($columns);
	$sql = make_prepare_insert_sql($table,$keys,$insert_ignore) ;
	$params = array_values($columns);
	// print_html($sql) ;

	$mysqli = new mysqli($dbs[$db_id]["host"],$dbs[$db_id]["user"],$dbs[$db_id]["pass"],$dbs[$db_id]["db_name"]);
	if (mysqli_connect_errno()) {
		if($configs["product_mode"] == false){
			printf("Connect failed: %s\n", mysqli_connect_error());
		}
		return false ;
	}
	$result = false ;
	$first = $params[0];
	if(is_array($first)){
		
	}else{
		if ($stmt = $mysqli->prepare($sql)) {
			$matrix = array();
			for($i=0;$i<count($params);$i++){
				$matrix["param_".$i] = $params[$i] ;
			}
			$args = to_binds(&$matrix);
			call_user_func_array(array(&$stmt, 'bind_param'), $args); 
			$stmt->execute();
			$result = $stmt->affected_rows ;
			$stmt->close();
		}else{
			
		}
	}
	$mysqli->close();
	return $result ;
}

function mi_delete($sql,$params=array(),$db_id="default"){
	global $dbs , $configs ;
	
	if(is_string($params)){
		$params = explode(",",$params);
	}

	$mysqli = new mysqli($dbs[$db_id]["host"],$dbs[$db_id]["user"],$dbs[$db_id]["pass"],$dbs[$db_id]["db_name"]);
	if (mysqli_connect_errno()) {
		if($configs["product_mode"] == false){
			printf("Connect failed: %s\n", mysqli_connect_error());
		}
		return false ;
	}
	$result = false ;
	if ($stmt = $mysqli->prepare($sql)) {
		if(!empty($params)){
			$matrix = array();
			for($i=0;$i<count($params);$i++){
				$matrix["param_".$i] = $params[$i] ;
			}
			$args = to_binds(&$matrix);
			call_user_func_array(array(&$stmt, 'bind_param'), $args); 
		}
		$stmt->execute();
		$result = $stmt->affected_rows ;
		$stmt->close();
	}

	$mysqli->close();
	return $result ;
}

function mi_select_line($sql,$params=array(),$db_id="default"){
	$result = mi_select($sql,$params,$db_id);
	if(count($result) > 0){
		return $result[0];
	}
	return null;
}

function mi_select($sql,$params=array(),$db_id="default"){
	global $dbs , $configs ;
	
	if(is_string($params)){
		$params = explode(",",$params);
	}
	if(is_int($params)){
		$params = array($params);
	}

	$result = array();
	$mysqli = new mysqli($dbs[$db_id]["host"],$dbs[$db_id]["user"],$dbs[$db_id]["pass"],$dbs[$db_id]["db_name"]);
	if (mysqli_connect_errno()) {
		if($configs["product_mode"] == false){
			printf("Connect failed: %s\n", mysqli_connect_error());
		}
		return $result ;
	}

	if ($stmt = $mysqli->prepare($sql)) {
		if(!empty($params)){
			$matrix = array();
			for($i=0;$i<count($params);$i++){
				$matrix["param_".$i] = $params[$i] ;
			}
			$args = to_binds(&$matrix);
			call_user_func_array(array(&$stmt, 'bind_param'), $args); 
			//$city = 1 ;
			//$stmt->bind_param("i", $city);
		}
		$stmt->execute();
		$data = $stmt->get_result();
		

		$fields = mysqli_fetch_fields($data) ;
		// print_html(json_encode($fields));
		$row_index = 0 ;

		while ($row = $data->fetch_array(MYSQLI_NUM)){
			$result[$row_index] = array();
			for($i=0;$i<count($fields);$i++){
				$field = $fields[$i]->name ;
				// print_html( "$field : " . $row[$i] . " , ") ;
				$result[$row_index][$field] = $row[$i];
			}
			$row_index++ ;
        }
		$stmt->close();
	}

	$mysqli->close();
	return $result ;
}

function get_mysqli_bind_type($v){
	if(is_int($v) || is_real($v)){
		return 'i';
	}
	if(is_float($v) || is_double($v)){
		return 'd' ;
	}
	return 's';
}

function get_mysqli_bind_types($params){
	$string = '';
	for($i=0;$i<count($params);$i++){
		$string .= get_mysqli_bind_type($params[$i]);
	}
	return $string ;
}

function to_binds($params){
	$args = array();
	$args[] = get_mysqli_bind_types(array_values($params)) ;
	
	$keys = array_keys($params) ;
	for($i=0;$i<count($keys);$i++){
		$key = $keys[$i] ;
		$args[] = &$params[$keys[$i]];
		// $params[$key] = null ;
	}
	// print_r($args);
	return $args ;
}

function make_prepare_insert_sql($table,$columns,$insert_ignore=false){
	$sql = "insert " . ($insert_ignore?" ignore ":"") . " into $table (" ;
	$sql .= implode(",",$columns);
	$sql .= ") values (";
	for($i=0;$i<count($columns);$i++){
		$sql .= $i > 0 ? "," : "" ;
		$sql .= "?" ;
	}
	$sql .= ")";
	return $sql ;
}

function make_prepare_update_sql($table,$columns,$limit){
	$sql = "update $table set " ;
	for($i=0;$i<count($columns);$i++){
		$sql .= $i > 0 ? "," : "" ;
		$sql .= $columns[$i] . "=?" ;
	}
	$sql .= " where " ;
	$sql .= $limit ;
	return $sql ;
}

